
package net.swa.dewei.service.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Service;
import net.swa.dewei.beans.entity.MenDian;
import net.swa.dewei.beans.vo.MendDistr;
import net.swa.dewei.service.MendDistributionService;
import net.swa.system.dao.HibernateDaoSupport;
import net.swa.util.BuzConst;

@Service("mendDistributionService")
public class MendDistributionServiceImpl extends HibernateDaoSupport implements MendDistributionService
{

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryMend(Boolean ischarge , Boolean weixiu , Boolean xiaoshou)
    {
        StringBuilder sql = new StringBuilder();
        Query query = null;
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");
        sql.append(" and m.status=" + BuzConst.STATUS_YES + " and(1=0 ");

        if((null==ischarge||!ischarge)&&(null==weixiu||!weixiu)&&(null==xiaoshou||!xiaoshou)){
            sql.append(" or 1=1  ");
        }else{
        if (null != ischarge&&ischarge)
        {
            sql.append(" or m.chargeStation=:ischarge");
        }

        if (null != weixiu&&weixiu)
        {
            sql.append(" or m.weixiu=:weixiu");
        }

        if (null != xiaoshou&&xiaoshou)
        {
            sql.append(" or m.xiaoshou=:xiaoshou");
        }
        }
        sql.append(" )");
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl",
                StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou", StandardBasicTypes.BOOLEAN).addScalar("mendianNo", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (null != ischarge&&ischarge)
        {
            query.setBoolean("ischarge", ischarge);
        }

        if (null != weixiu&&weixiu)
        {
            query.setBoolean("weixiu", weixiu);
        }

        if (null != xiaoshou&&xiaoshou)
        {
            query.setBoolean("xiaoshou", xiaoshou);
        }
        List<MenDian> list = query.list();
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MendDistr> queryMendDistr(Boolean ischarge , Boolean weixiu , Boolean xiaoshou)
    {
        StringBuilder sql = new StringBuilder();
        Query query = null;
        sql.append("select sum(totalNum) as totalNum, if(province='','未指定省份',province) province  FROM(select sum(totalNum) as totalNum,  ifnull(province,'') province FROM(");
        sql.append("select count(m.id) as totalNum, trim( m.province) province from  t_MenDian m  where 1=1 ");
        sql.append(" and m.status=" + BuzConst.STATUS_YES + " and (1=0  ");
        
        if((null==ischarge||!ischarge)&&(null==weixiu||!weixiu)&&(null==xiaoshou||!xiaoshou)){
            sql.append(" or 1=1  ");
        }else{
            if (null != ischarge&&ischarge)
            {
                sql.append(" or m.chargeStation=:ischarge");
            }

            if (null != weixiu&&weixiu)
            {
                sql.append(" or m.weixiu=:weixiu");
            }

            if (null != xiaoshou&&xiaoshou)
            {
                sql.append(" or m.xiaoshou=:xiaoshou");
            }
        }
        sql.append(" )");
        sql.append(" GROUP BY province) d GROUP BY province) dd GROUP BY province");
        
        
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("totalNum", StandardBasicTypes.INTEGER).addScalar("province", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MendDistr.class));

        if (null != ischarge&&ischarge)
        {
            query.setBoolean("ischarge", ischarge);
        }

        if (null != weixiu&&weixiu)
        {
            query.setBoolean("weixiu", weixiu);
        }

        if (null != xiaoshou&&xiaoshou)
        {
            query.setBoolean("xiaoshou", xiaoshou);
        }
        List<MendDistr> list = query.list();
        return list;
    }
}